package net.baiken.yuki;

import java.awt.AWTException;
import java.awt.MenuItem;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URLClassLoader;
import java.util.Locale;
import java.util.Properties;
import java.util.Map.Entry;

import javax.swing.JFrame;

import net.baiken.commons.Messages;
import net.baiken.messages.YukiMessageKeys;
import net.baiken.yuki.svn.SVNServer;

public class Yuki extends TrayIcon implements ActionListener {

	private static final String SVN_ON_ICON_NAME = "net/baiken/images/svn-on.png";

	private static final String SVN_OFF_ICON_NAME = "net/baiken/images/svn-off.png";

	private static boolean messagesAreEnable;

	private AboutFrame aboutFrame;

	public Yuki() {
		super(Toolkit.getDefaultToolkit().createImage(
				ClassLoader.getSystemResource(SVN_ON_ICON_NAME)), Messages
				.getInstance().getString(YukiMessageKeys.TitleKey),
				new YukiMenu());

		initMenuItems();
		aboutFrame=new AboutFrame();
	}

	private void initMenuItems() {
		for (int i = 0; i < getPopupMenu().getItemCount(); i++) {
			MenuItem menuItem = getPopupMenu().getItem(i);
			menuItem.addActionListener(this);
		}
		changeStatus(false);
	}

	private void changeStatus(boolean isStopped) {
		((YukiMenu) getPopupMenu()).getStartSVNMenuItem()
				.setEnabled(!isStopped);
		((YukiMenu) getPopupMenu()).getStopSVNMenuItem().setEnabled(isStopped);
		setImage(Toolkit.getDefaultToolkit().getImage(
				isStopped ? ClassLoader.getSystemResource(SVN_ON_ICON_NAME)
						: ClassLoader.getSystemResource(SVN_OFF_ICON_NAME)));
	}

	public static void main(String[] args) throws AWTException,
			FileNotFoundException, IOException, URISyntaxException {
		{// Initialization
			String path = new File(((URLClassLoader) Thread.currentThread()
					.getContextClassLoader()).getURLs()[0].toURI().getPath())
					.getParentFile().getAbsolutePath();
			Properties properties = new Properties();
			properties.load(new FileReader(new File(path, "yuki.properties")));

			for (Entry<Object, Object> key : properties.entrySet()) {
				System.setProperty("" + key.getKey(), "" + key.getValue());
			}
			System.setProperty("yuki.runpath", path);
			messagesAreEnable = Boolean.valueOf(System
					.getProperty("yuki.messages.enable"));

			Locale.setDefault(new Locale(System.getProperty("yuki.locale")));
			Messages.getInstance().setBaseName(
					"net.baiken.messages.MessageResource");
		}
		SystemTray.getSystemTray().add(new Yuki());
		((Yuki) SystemTray.getSystemTray().getTrayIcons()[0]).displayMessage(
				Messages.getInstance().getString(
						YukiMessageKeys.ApplicationStarted), MessageType.INFO);
		
		
	}

	private void displayMessage(String message, MessageType messageType) {
		if (messagesAreEnable) {
			displayMessage(Messages.getInstance().getString(
					YukiMessageKeys.TitleKey), message, messageType);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		YukiCommand command = YukiCommand.valueOf(e.getActionCommand());
		if (command == YukiCommand.Exit) {
			System.exit(0);
		} else if (command == YukiCommand.StartSVN) {
			SVNServer.getInstance().startSVN();
			changeStatus(true);
			displayMessage(Messages.getInstance().getString(
					YukiMessageKeys.Message.StartSVN), MessageType.INFO);
			System.out.println("Yuki.actionPerformed()::" + command);
		} else if (command == YukiCommand.StopSVN) {
			SVNServer.getInstance().stopSVN();
			changeStatus(false);
			displayMessage(Messages.getInstance().getString(
					YukiMessageKeys.Message.StopSVN), MessageType.INFO);
			System.out.println("Yuki.actionPerformed()::" + command);
		} else if (command == YukiCommand.About) {
			aboutFrame.setVisible(true);
			System.out.println("Yuki.actionPerformed()::" + command);
		}
	}
}
